<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>科泰聊天室</title>
  <link href="css/chat.css" rel="stylesheet">
  </link>
</head>

<body>
  <!-- TODO 根据HTML结构分析，定义出父组件和子组件结构 -->
  <div id="app">
    <h1 class="title center">科泰聊天室</h1>
    <text-father></text-father>
  </div>

  <template id="textFather">
    <div class="chat-window center">
      <div class="chat-messages">
        <div :class="{'message': true, [cht.type + '-message']: true}" v-for="(cht,index) in chat" :key="index">
          <div :class="{['message-'+cht.type]:true}"><img :src="cht.avatar" class="avatar"
              v-show="cht.type == 'received'"> <span class="message-content">{{cht.content}}</span><img
              :src="cht.avatar" class="avatar" v-show="cht.type == 'sent'"></div>
        </div>
      </div>
      <text-son @danji="change" :newText="newText"></text-son>
    </div>
  </template>

  <template id="textSon">
    <div class="chat-input"><input type="text" placeholder="请输入消息..." v-model="newText"> <button
        @click="sonClick()">发送</button></div>
  </template>

  <script src="js/vue.js"></script>
  <script>
    // TODO 根据HTML结构分析，定义出父组件和子组件结构
    var textSon = {
      template: '#textSon',
      props: ['newText'],
      methods: {
        sonClick() {
          this.$emit("danji", this.newText)
        }
      }
    }

    var textFather = {
      template: '#textFather',
      components: {
        'TextSon': textSon
      },
      data() {
        return {
          chat: [
            {
              type: "received",
              avatar: "images/avt-01.png",
              content: "好久不见！"
            },
            {
              type: "sent",
              avatar: "images/avt-02.png",
              content: "是啊，好久不见了"
            },
            {
              type: "received",
              avatar: "images/avt-01.png",
              content: "最近怎么样？"
            }
          ],
          newText: ''
        }
      },
      methods: {
        change(value) {
          this.newText = value;
          this.chat.push({
            type: "sent",
            avatar: "images/avt-02.png",
            content: this.newText
          })
        }
      }
    }

    new Vue({
      el: '#app',
      components: {
        'TextFather': textFather
      }
    })

  </script>
</body>

</html>